package com.hmh.student.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmh.student.system.entity.SysMenu;
import com.hmh.student.system.entity.SysRole;
import com.hmh.student.system.service.ISysMenuService;
import com.hmh.student.system.service.ISysRoleMenuService;
import com.hmh.student.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyf
 * @since 2025-04-15
 */
@RestController
@RequestMapping("/sysRole")



public class SysRoleController {


    @Autowired
   private ISysRoleService sysRoleService;
    @Autowired
   private ISysMenuService sysMenuService;
    @Autowired
   private ISysRoleMenuService sysRoleMenuService;


//新增
    @PostMapping("/add")
    public Map<String,Object> testRoleAdd(@RequestBody SysRole sysRole) {
        Map<String,Object> map=new HashMap<>();


        try {
            sysRoleService.save(sysRole);
            map.put("msg","success");

        } catch (Exception e) {
            map.put("msg","fail");
        }

        return map;
    }

    //修改
    @PutMapping("/RoleUpdate")
    public Map<String,Object> testRoleUpdate(@RequestBody SysRole sysRole) {
        Map<String, Object> map = new HashMap<>();
        try {
            sysRoleService.updateById(sysRole);

            map.put("msg","success");
        } catch (Exception e) {

            map.put("msg","注册失败"+e.getMessage());
        }
               return map;
    }

    //删除
    @DeleteMapping("/RoleDelete")
    public Map<String,Object> testRoleDelete(@RequestParam Long roleId) {
        Map<String, Object> map = new HashMap<>();


        try {
            sysRoleService.removeById(roleId);
            map.put("msg","success");
        } catch (Exception e) {
            map.put("msg","删除失败"+e.getMessage());
        }

        return map;
    }

    //分页查询
  @GetMapping("/pageRole")
        public Page<SysRole> pageRole(@RequestParam (required = false) String roleName,
                                      @RequestParam (defaultValue = "10")Integer pageSize,
                                      @RequestParam (defaultValue = "1")Integer current) {

      Page<SysRole> page = new Page<>(current, pageSize);

      Page<SysRole> sysRolePage = sysRoleService.queryRolePage(page,roleName);


      return sysRolePage;
  }


        // 获取角色拥有的菜单ID列表

    @GetMapping("/getRoleMenus")
    public Map<String, Object> getRoleMenus(@RequestParam(required = false) Integer roleId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Integer> menuIds = sysRoleMenuService.getMenuIdsByRoleId(roleId);
            result.put("success", true);
            result.put("data", menuIds);
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "获取角色菜单失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 获取所有菜单树形结构
     */
    @GetMapping("/getMenuTree")
    public Map<String, Object> getMenuTree() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, List<SysMenu>> menuMap = sysMenuService.getAllMenu();
            // 构建树形结构
            List<Map<String, Object>> treeData = buildMenuTree(menuMap, "0");
            result.put("success", true);
            result.put("data", treeData);
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "获取菜单树失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 保存角色菜单
     */
    @PostMapping("/saveRoleMenus")
    public Map<String, Object> saveRoleMenus(@RequestBody Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer roleId = (Integer) params.get("roleId");
            @SuppressWarnings("unchecked")
            List<Integer> menuIds = (List<Integer>) params.get("menuIds");

            boolean success = sysRoleMenuService.saveRoleMenus(roleId, menuIds);
            if (success) {
                result.put("success", true);
                result.put("msg", "保存角色菜单成功");
            } else {
                result.put("success", false);
                result.put("msg", "保存角色菜单失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "保存角色菜单异常: " + e.getMessage());
        }
        return result;
    }

    // 辅助方法：构建菜单树形结构
    private List<Map<String, Object>> buildMenuTree(Map<String, List<SysMenu>> menuMap, String parentId) {
        List<Map<String, Object>> treeNodes = new ArrayList<>();
        List<SysMenu> menus = menuMap.get(parentId);
        if (menus != null) {
            for (SysMenu menu : menus) {
                Map<String, Object> node = new HashMap<>();
                node.put("id", menu.getMenuId());
                node.put("label", menu.getMenuName());
                node.put("children", buildMenuTree(menuMap, String.valueOf(menu.getMenuId())));
                treeNodes.add(node);
            }
        }
        return treeNodes;
    }

    /*
    * 获得所有角色
    * *//*
    @GetMapping("/getAllRole")
    public  Map<String,List<SysRole>> getAllRole() {
        List<SysRole> sysRoles = sysRoleService.list();
        Map<String,List<SysRole>> map = new HashMap<>();
        for (SysRole sysRole : sysRoles) {

            String roleId =String.valueOf(sysRole.getRoleId());

            if (!map.containsKey(roleId)) {
                map.put(roleId, new ArrayList<>());
            }
            map.get(roleId).add(sysRole);
        }
        return map;
    }*/

    }

